/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       progress_bar.cpp

	$Header: /game/progress_bar.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "progress_bar.h"

#include "bitmap_group_cache.h"
#include "bitmap_group.h"
#include "bitmap_layer_window.h"
#include "game_time.h"
#include "platform.h"

// ----------------------------------------------------------------------
// progress bar control
// ----------------------------------------------------------------------
static t_bitmap_group_cache k_bitmaps( "icons.progress_bar" );

t_progress_bar::t_progress_bar( t_screen_point point, t_window* parent, char const* bar_layer ) 
	          : t_window( k_completely_transparent, parent )
{
	t_window*             window;
	t_bitmap_layer const* layer;
	t_screen_point        origin(0,0);
	t_screen_rect         bar_rect;

	m_bitmaps = k_bitmaps.get();
	layer = m_bitmaps->find( bar_layer );
	assert(layer != NULL);
	m_bar = new t_bitmap_layer_window( layer, origin, this );
	bar_rect = layer->get_rect();
	m_maximum_width = bar_rect.width();
	bar_rect.right = bar_rect.left;
	m_bar->move( bar_rect );
	layer = m_bitmaps->find( "top" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_bitmaps->find( "bottom" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_bitmaps->find( "left" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_bitmaps->find( "right" );
	window = new t_bitmap_layer_window( layer, origin, this );
	m_value = 0;
	m_maximum = 0;
	m_last_update = get_time();
	init( m_bitmaps->get_rect() );
}

// ----------------------------------------------------------------------
// progress bar control
// ----------------------------------------------------------------------
void t_progress_bar::update_progress()
{
	t_screen_rect bar_rect = to_client( m_bar->get_screen_rect() );
	int           width;

	if (m_maximum == 0 || m_value <= 0)
		width = 0;
	else if (m_value > m_maximum)
		width = m_maximum_width;
	else
		width = m_value * m_maximum_width / m_maximum;
	if (bar_rect.width() == width)
		return;
	bar_rect.right = bar_rect.left + width;
	m_bar->move( bar_rect );
	if (elapsed_time( m_last_update ) > 100)
	{
		redraw_windows();
		m_last_update = get_time();
	}
}
